﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Text;
using NUnit.Framework;
using SD.Entities;
using SD.Commons;
using SD.Commons.Helpers;
using SD.DataAccess;

namespace SD.Test
{
    [TestFixture]
    public class TestContact
    {
        ContactAccess contactAccess = new ContactAccess();
       
        [Test]
        public void TestListContactById_InputInvalidContactId_ReturnNull()
        {
            object expected = null;
            
            var actual = contactAccess.ListContactById(-1);
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TestListContactById_InputValidContactId_ReturnContact()
        {
            var expected = new ContactEntity()
                {
                    FirstName = "Anh"
                    ,
                    Surname = "Hoang"
                    ,
                    KnownAs = "Duc"
                    ,
                    OfficePhone = "0984432425"
                    ,
                    MobilePhone = "1234567890"
                    ,
                    StHomePhone = "22222222222"
                    ,
                    Email = "email@abc.def"
                    ,
                    ManagerId = 1
                    ,
                    ContactTypeCode = 4
                    ,
                    BestContactMethodCode = 7
                    ,
                    JobRole = "programer"
                    ,
                    Workbase = "Information"
                    ,
                    JobTitle = "Technology"
                    ,
                    IsActive = true
                };
            var dt = contactAccess.ListContactById(1);
            var actual = new ContactEntity()
                {
                    FirstName = dt.Rows[0]["FirstName"].ToString()
                    ,
                    Surname = dt.Rows[0]["Surname"].ToString()
                    ,
                    KnownAs = dt.Rows[0]["KnownAs"].ToString()
                    ,
                    OfficePhone = dt.Rows[0]["OfficePhone"].ToString()
                    ,
                    MobilePhone = dt.Rows[0]["MobilePhone"].ToString()
                    ,
                    StHomePhone = dt.Rows[0]["StHomePhone"].ToString()
                    ,
                    Email = dt.Rows[0]["Email"].ToString()
                    ,
                    ManagerId = int.Parse(dt.Rows[0]["ManagerId"].ToString())
                    ,
                    ContactTypeCode = int.Parse(dt.Rows[0]["ContactTypeCode"].ToString())
                    ,
                    BestContactMethodCode = int.Parse(dt.Rows[0]["BestContactMethodCode"].ToString())
                    ,
                    JobRole = dt.Rows[0]["JobRole"].ToString()
                    ,
                    Workbase = dt.Rows[0]["Workbase"].ToString()
                    ,
                    JobTitle = dt.Rows[0]["JobTitle"].ToString()
                    ,
                    IsActive = (bool)dt.Rows[0]["IsActive"]
                };

            if (!expected.FirstName.Equals(actual.FirstName))
            {
                Assert.Fail();                
            }
            if (!expected.Surname.Equals(actual.Surname))
            {
                Assert.Fail();
            }
            if (!expected.KnownAs.Equals(actual.KnownAs))
            {
                Assert.Fail();
            } if (!expected.OfficePhone.Equals(actual.OfficePhone))
            {
                Assert.Fail();
            }
            if (!expected.MobilePhone.Equals(actual.MobilePhone))
            {
                Assert.Fail();
            }
            if (!expected.StHomePhone.Equals(actual.StHomePhone))
            {
                Assert.Fail();
            }
            if (!expected.Email.Equals(actual.Email))
            {
                Assert.Fail();
            }
            if (!expected.ManagerId.Equals(actual.ManagerId))
            {
                Assert.Fail();
            }
            if (!expected.ContactTypeCode.Equals(actual.ContactTypeCode))
            {
                Assert.Fail();
            }
            if (!expected.BestContactMethodCode.Equals(actual.BestContactMethodCode))
            {
                Assert.Fail();
            }
            if (!expected.JobRole.Equals(actual.JobRole))
            {
                Assert.Fail();
            }
            if (!expected.Workbase.Equals(actual.Workbase))
            {
                Assert.Fail();
            }
            if (!expected.JobTitle.Equals(actual.JobTitle))
            {
                Assert.Fail();
            }
            if (!expected.IsActive.Equals(actual.IsActive))
            {
                Assert.Fail();
            }
            Assert.Pass();
        }

        [Test]
        public void TestLoadDropDownList_InputInvalidCode_ReturnNull()
        {
            object expected = null;

            var actual = contactAccess.LoadDropDownList(-1);
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TestLoadDropDownList_InputValidCode_ReturnReferenceData()
        {
           var expected = new string[] {"Director", "Manager", "Staff"};

            var dt = contactAccess.LoadDropDownList(1);
            var actual = new string[]
                {
                    dt.Rows[0]["ReferenceDataValue"].ToString()
                    ,dt.Rows[1]["ReferenceDataValue"].ToString()
                    ,dt.Rows[2]["ReferenceDataValue"].ToString()
                };

            Assert.AreEqual(expected,actual);
        }

        [Test]
        public void TestLoadGridView_ReturnRowsCount()
        {
            object expected = 36;

            var dt = contactAccess.LoadGridView("","",0,"");
            var actual = dt.Rows.Count;
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TestAddContact_InputWithout_ReturnFalse()
        {
            var contact = new ContactEntity()
                {
                    FirstName = "Anh"
                };
            var expected = false;
            var actual = contactAccess.AddContact(contact);
            Assert.AreEqual(expected, actual);
        }
        
        [Test]
        public void TestAddContact_InputFull_ReturnTrue()
        {
            var contact = new ContactEntity()
            {
                FirstName = "Anh",
                Surname = "Duc",
                ContactTypeCode = 4,
                Email = "ABC@def.ghj",
                KnownAs = "",
                OfficePhone = "",
                BestContactMethodCode = -1,
                IsActive = true,
                JobRole = "",
                JobTitle = "",
                ManagerId = 1,
                MobilePhone = "",
                StHomePhone = "",
                Workbase = "",                
            };
            var expected = true;
            var actual = contactAccess.AddContact(contact);
            Assert.AreEqual(expected, actual);
        }
    }
}
